import Vue from 'vue'
import store from '../store'

/**
 * v-has='资源code'
 * 使用指定判断是否有权限
 */
const action = Vue.directive('hasBtn', {

  inserted(el, binding, vnode) {
    let { expression } = binding
    expression=expression.replaceAll("'","")
    expression=expression.replaceAll('"',"")
    if (!hasPermission(expression)) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
})

/**
 * $_has('资源code') return true or false
 * 使用v-if判断是否有权限
 * 资源权限
 * @param resKey 资源Code
 */
const hasPermission = (resKey) => {
  const userroles = store.state.permission.showBtnPer
  if (userroles != null && userroles instanceof Array) {
    return userroles.includes(resKey)
  } else {
    return false
  }
}

// Vue.prototype.$_has = hasPermission
export { action }
